package com.ysu.zyw.service;

import com.ysu.zyw.dao.BlogArticleMapper;
import com.ysu.zyw.dao.BlogCategoryMapper;
import com.ysu.zyw.modal.po.BlogArticleEntity;
import com.ysu.zyw.other.exception.KnownException;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.annotation.security.RolesAllowed;
import javax.servlet.http.HttpServletRequest;
import java.io.*;
import java.util.UUID;

/**
 * Created by zhang_yao_wu1993 on 15/4/27.
 *
 * @author by Zerur
 */
@Service("blogUploadService")
public class BlogUploadService {
    private BlogCategoryMapper blogCategoryMapper;
    private BlogArticleMapper blogArticleMapper;

    public BlogCategoryMapper getBlogCategoryMapper() {
        return blogCategoryMapper;
    }

    @SuppressWarnings("all")
    @Resource
    public void setBlogCategoryMapper(BlogCategoryMapper blogCategoryMapper) {
        this.blogCategoryMapper = blogCategoryMapper;
    }

    public BlogArticleMapper getBlogArticleMapper() {
        return blogArticleMapper;
    }

    @SuppressWarnings("all")
    @Resource
    public void setBlogArticleMapper(BlogArticleMapper blogArticleMapper) {
        this.blogArticleMapper = blogArticleMapper;
    }

    @RolesAllowed("ADMIN")
    @Transactional
    public void addCategory(String category) {
        blogCategoryMapper.insertCategoryEntity(category);
    }

    @Transactional
    @RolesAllowed("ROLE_ADMIN")
    public void addArticle(String title, String categoryIdFromForm, MultipartFile file, HttpServletRequest request) throws IOException {
        if(title.equals("") || categoryIdFromForm.equals("")) {
            throw new KnownException("信息有误");
        }

        // 获得 categoryId
        Integer categoryId = Integer.parseInt(categoryIdFromForm);

        // 保存文件
        String uuid = UUID.randomUUID().toString();
        this.saveHtml(request, file, uuid);

        // 构造对象
        BlogArticleEntity blogArticleEntity = new BlogArticleEntity(title, uuid, categoryId);

        // 保存
        blogArticleMapper.insertBlogArticleEntity(blogArticleEntity);
    }

    private void saveHtml(HttpServletRequest request, MultipartFile file, String uuid) throws IOException {
        String wizHtmlPath = request.getSession().getServletContext().getRealPath("/WEB-INF/view/wiz-html");
        File wizHtmlFile = new File(wizHtmlPath + "/" + uuid + ".jsp");

        // 初始化出入流
        OutputStream outputStream = new FileOutputStream(wizHtmlFile);
        OutputStreamWriter outputStreamWriter = new OutputStreamWriter(outputStream);
        InputStream inputStream = file.getInputStream();
        InputStreamReader inputStreamReader = new InputStreamReader(inputStream);

        // 读入所有的 html
        StringBuilder wizHtmlStringBuilder = new StringBuilder();
        int i = 0;
        while((i = inputStreamReader.read()) != -1) {
            wizHtmlStringBuilder.append((char) i);
        }

        // 对 stylesheet 做路径替换
        String wizHtmlString  = wizHtmlStringBuilder.toString().replaceAll("<link type=\"text/css\" rel=\"stylesheet\" id=\"wiz_code_highlight_link\" href=\".+?\">",
                "<link type=\"text/css\" rel=\"stylesheet\" id=\"wiz_code_highlight_link\" href=\"/Other/StyleSheet/wiz_code_highlight.css\">");

        // 写入 html
        outputStreamWriter.write(wizHtmlString);

        // close 流
        inputStreamReader.close();
        outputStreamWriter.close();
    }
}
